package com.example.myapplication1;

import android.app.AppComponentFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;

public class RelativelayoutActivity extends AppCompatActivity {

    private List<Integer>pics=new ArrayList<>();
    private ViewPager2 mViewPager2;
    private  LinearLayout dots;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relativelayout);

        pics.add(R.drawable.p1);
        pics.add(R.drawable.p2);
        pics.add(R.drawable.p1);

        RecyclerView.Adapter adapter = new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(RelativelayoutActivity.this).inflate(R.layout.item_layout,parent,false);
                return new MyViewHolder(view);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                LinearLayout container=((MyViewHolder)holder).container;
                container.setBackgroundResource(pics.get(position));
            }

            @Override
            public int getItemCount() {
                return pics.size();
            }
        };


        mViewPager2 = findViewById(R.id.vp1);
        mViewPager2.setAdapter(adapter);
        dots=findViewById(R.id.dots);
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for(int i=0;i<dots.getChildCount();i++){
                    ((ImageView)dots.getChildAt(i)).setImageResource(R.drawable.dot_unselected);
                }
                ((ImageView)dots.getChildAt(position)).setImageResource(R.drawable.dot_selected);
            }
        });
        }


    private class MyViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout container;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            container = (LinearLayout)itemView;
        }
    }

}
